package com.cqeec.addressbook.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cqeec.addressbook.entity.ContactNameInfo;
import com.cqeec.addressbook.service.ContactNameInfoService;

@Controller
@RequestMapping("/app/nameinfo/")
public class ContactNameInfoController {
	
	@Autowired
	private ContactNameInfoService contactnameinfoservice;
	
	
	@RequestMapping("/delete.html")
	public ModelAndView delete(String  page,String size,String id) {
		ModelAndView mav = new ModelAndView("redirect:/app/nameinfo/list.html");
		int pageNumber = (page == null || "".equals(page)) ? 1 : Integer.parseInt(page);
		int pageSize = (size == null || "".equals(size)) ? 2 : Integer.parseInt(size);
		try {
			contactnameinfoservice.delete(id);
			mav.addObject("mseeage", "删除数据成功");
		} catch(Exception e) {
			e.printStackTrace();
			mav.addObject("message", "删除数据失败！");
		}
		mav.addObject("page", pageNumber);
		mav.addObject("size", pageSize);
		return mav;
	}
	
	@RequestMapping("/update.html")
	public ModelAndView update(String page,String size, ContactNameInfo contactnameInfo) {
		ModelAndView mav = new ModelAndView("nameinfo/edit");
		int pageNumber = (page == null || "".equals(page)) ? 1 : Integer.parseInt(page);
		int pageSize = (size == null || "".equals(size)) ? 2 : Integer.parseInt(size);
		try {
			contactnameinfoservice.update(contactnameInfo);
			mav.addObject("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("message", "保存数据失败！");
		}
		mav.addObject("obj", contactnameInfo);
		mav.addObject("page", pageNumber);
		mav.addObject("size", pageSize);
		return mav;
	}
	@RequestMapping("/save.html")
	public ModelAndView save(String page,String size,ContactNameInfo contactnameInfo,RedirectAttributes ra) {
		ModelAndView mav = new ModelAndView("redirect:/app/nameinfo/add.html");
		int pageNumber = (page == null || "".equals(page)) ? 1 : Integer.parseInt(page);
		int pageSize = (size == null || "".equals(size)) ? 2 : Integer.parseInt(size);
		String id = contactnameInfo.getId();
		if(id==null || "".equals(id)) {
			contactnameInfo.setId(UUID.randomUUID().toString().toUpperCase());
		}
		try {
			contactnameinfoservice.insert(contactnameInfo);
			ra.addFlashAttribute("message", "保存数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			ra.addFlashAttribute("message", "保存数据失败！");
		}
		ra.addFlashAttribute("page", pageNumber);
		ra.addFlashAttribute("size", pageSize);
		return mav;
	}
	
	@RequestMapping("/list.html")
	public ModelAndView list(String page,String size,String name) {
		ModelAndView mav = new ModelAndView("nameinfo/list");
		int pageNumber, pageSize;
		pageNumber = (page == null || "".equals(page)) ? 1 : Integer.parseInt(page);
		pageSize = (size == null || "".equals(size)) ? 2 : Integer.parseInt(size);
		
		long total;
		ContactNameInfo contactnameInfo = new ContactNameInfo();
		if (name == null || "".equals(name)) {
			// 没有条件查询
			total = contactnameinfoservice.count();
		} else {
			// 条件查询
			contactnameInfo.setName("%"+name+"%");
			total = contactnameinfoservice.count(contactnameInfo);
		}
		int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
		if(total==0) {
			pageNumber=0;
		}else {
			if(pageNumber>pages) {
			   pageNumber = pages;
			}
			List<ContactNameInfo> list;
			if(name==null || "".equals(name)) {
				// 没有条件查询
				list=contactnameinfoservice.findPage(pageNumber, pageSize);
			}else {
				//条件查询
				list=contactnameinfoservice.findByConditionPage(pageNumber, pageSize, contactnameInfo);
			}
			mav.addObject("list", list);
		}
		mav.addObject("pages", pages);
		mav.addObject("total", total);
		mav.addObject("page", pageNumber);
		mav.addObject("size", pageSize);
		mav.addObject("name", name);
		return mav;
	}
	
	@RequestMapping("/add.html")
	public String gotoAddPage(String page,String size,Model model) {
		int pageNumber = (page == null || "".equals(page)) ? 1 : Integer.parseInt(page);
		int pageSize = (size == null || "".equals(size)) ? 2 : Integer.parseInt(size);
		model.addAttribute("page", pageNumber);
		model.addAttribute("size", pageSize);
		return "nameinfo/add";
	}
	
	@RequestMapping("/edit.html")
	public String gotoEditPage(String id,String page,String size,Model model) {
		int pageNumber = (page == null || "".equals(page)) ? 1 : Integer.parseInt(page);
		int pageSize = (size == null || "".equals(size)) ? 2 : Integer.parseInt(size);
		ContactNameInfo contactnameInfo = contactnameinfoservice.findById(id);
		model.addAttribute("obj",  contactnameInfo);
		model.addAttribute("page", pageNumber);
		model.addAttribute("size", pageSize);
		return "nameinfo/edit";
	}
	
}
